import tkinter as tk
import pyautogui
from threading import Thread
import time

class FloatingButton:
    def __init__(self):
        self.root = tk.Tk()
        self.root.overrideredirect(True)  # 移除窗口边框
        self.root.attributes('-topmost', True)  # 窗口始终置顶
        # 添加透明度支持
        self.root.attributes('-transparentcolor', 'black')
        # 添加点击穿透属性
        self.root.attributes('-alpha', 0.7)  # 略微透明以显示下方窗口
        
        # 增加画布尺寸到 26x26
        self.button = tk.Canvas(self.root, width=26, height=26, bg='black',
                              highlightthickness=0)
        self.create_beautiful_circle('red')
        self.button.pack()
        
        # 添加点击计数变量
        self.clicking = False
        self.click_thread = None
        self.click_count = 0
        
        # 绑定鼠标事件用于拖动
        self.button.bind('<Button-1>', self.start_move)
        self.button.bind('<B1-Motion>', self.on_move)
        
        # 绑定空格键
        self.root.bind('<space>', self.start_clicking)
        self.root.bind('<Button-3>', self.stop_clicking)
        
        # 设置初始位置
        self.root.geometry('+100+100')
        
        # 添加退出处理
        self.root.protocol("WM_DELETE_WINDOW", self.on_closing)
        # 绑定 Ctrl+C 退出
        self.root.bind('<Control-c>', self.on_closing)
        # 绑定 Esc 键退出
        self.root.bind('<Escape>', self.on_closing)

    def start_move(self, event):
        self.x = event.x
        self.y = event.y

    def on_move(self, event):
        deltax = event.x - self.x
        deltay = event.y - self.y
        x = self.root.winfo_x() + deltax
        y = self.root.winfo_y() + deltay
        self.root.geometry(f'+{x}+{y}')

    def auto_click(self):
        while self.clicking:
            try:
                # 保存当前位置
                current_x = self.root.winfo_x()
                current_y = self.root.winfo_y()
                
                # 完全隐藏窗口
                self.root.withdraw()
                # 确保窗口完全隐藏
                time.sleep(0.05)
                
                # 计算点击位置（圆圈中心）
                click_x = current_x + 13
                click_y = current_y + 13
                
                # 执行点击
                pyautogui.click(x=click_x, y=click_y, button='left')
                
                # 恢复窗口显示
                self.root.deiconify()
                # 恢复到原来的位置
                self.root.geometry(f'+{current_x}+{current_y}')
                
                self.click_count += 1
                # 处理事件，使空格键能够被响应
                self.root.update()
                
                time.sleep(0.1)
            except:
                self.root.deiconify()  # 确保异常时窗口能够恢复显示
                break

    def start_clicking(self, event):
        """空格键开始点击"""
        if not self.clicking:
            self.clicking = True
            self.create_beautiful_circle('green')
            self.click_thread = Thread(target=self.auto_click)
            self.click_thread.daemon = True
            self.click_thread.start()

    def stop_clicking(self, event):
        """右键停止点击"""
        if self.clicking:
            self.clicking = False
            self.create_beautiful_circle('red')
            print(f"本次共点击了 {self.click_count} 次")
            self.click_count = 0

    def create_beautiful_circle(self, color):
        """创建一个带渐变和边框效果的圆形"""
        self.button.delete("all")
        
        # 调整所有圆形的尺寸
        self.button.create_oval(1, 1, 25, 25, fill=color, outline=color)
        
        # 添加高光效果
        if color == 'red':
            highlight_color = '#ff6666'
        else:  # green
            highlight_color = '#66ff66'
            
        # 调整内部渐变光晕
        self.button.create_oval(5, 5, 21, 21, fill=highlight_color, outline=highlight_color)
        
        # 调整顶部高光位置和大小
        self.button.create_oval(8, 7, 13, 12, fill='white', outline='white', stipple='gray50')

    def on_closing(self, event=None):
        """处理程序退出"""
        if self.clicking:
            self.clicking = False
            print(f"本次共点击了 {self.click_count} 次")
        self.root.quit()

    def run(self):
        try:
            self.root.mainloop()
        except KeyboardInterrupt:
            self.on_closing()
        finally:
            print("程序已退出")

if __name__ == '__main__':
    pyautogui.FAILSAFE = True  # 启用自动防故障功能
    app = FloatingButton()
    app.run() 